function retry(){

}

function retry(fn,maxTimes = 3){
    return new Promise((resolve,reject) =>{
        const repeat = ()=>{
            fn().then((res)=>{
                resolve(res);
            }
    
            ).catch((rej)=>{
                if(--maxTimes<=0){
                    reject(rej);
                }else{
                    repeat();
                }
               
            })
        }
       
        repeat();
    })

}
function test(){
    return new Promise((resolve,reject)=>{
        setTimeout(()=>{
            const time = Math.floor(Math.random()*10)
            console.log(time);
            
            if(time<9){
                reject('失败')
            }else{
                resolve('Success');
            }
           
        },500);

    })
}
retry(test,3).then(res=>{
    console.log(res)

}).catch(err=>{
    console.log(err)

}
)
